home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / src / terminat.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-05  |  1.9 KB  |  86 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknowledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11.  
  12. #include "Termination.h"
  13. #include <osbind.h> // for Setexc()
  14. #include <signal.h>
  15. #include <stdlib.h>
  16.  
  17.  
  18. void SignalHandler(int SigNum)
  19. {
  20.     exit(SigNum);
  21. }
  22.  
  23. // Cancel the fact that we are in supervisor mode when an exception occurs
  24. #define UNRTE asm("unlk a6; addl #6,sp; movew 0x300,sr")
  25.  
  26. void RaiseILL() { UNRTE; SignalHandler(SIGILL); }
  27. void RaisePRIV() { UNRTE; SignalHandler(SIGPRIV); }
  28. void RaiseFPE() { UNRTE; SignalHandler(SIGFPE); }
  29. void RaiseBUS() { UNRTE; SignalHandler(SIGBUS); }
  30. void RaiseSEGV() { UNRTE; SignalHandler(SIGSEGV); }
  31.  
  32. static int Unixified=0;
  33.  
  34. typedef void (*exc)(void);
  35.  
  36. static exc OrgILL;
  37. static exc OrgPRIV;
  38. static exc OrgFPE;
  39. static exc OrgBUS;
  40. static exc OrgSEGV;
  41.  
  42. const BUS_ERR = 2;
  43. const ADDR_ERR = 3;
  44. const ILL_INSTR = 4;
  45. const DIV_ZERO = 5;
  46. // const RANGE_CHK = 6;
  47. // const OVFLW_CHK = 7;
  48. const PRIV_VIOL = 8;
  49.  
  50. void UnixifyAtariExceptions(int on)
  51. {
  52.     if (on!=Unixified) {
  53.         if (on) {
  54.             OrgILL=Setexc(ILL_INSTR,RaiseILL);
  55.             OrgPRIV=Setexc(PRIV_VIOL,RaisePRIV);
  56.             OrgFPE=Setexc(DIV_ZERO,RaiseFPE);
  57.             OrgBUS=Setexc(BUS_ERR,RaiseBUS);
  58.             OrgSEGV=Setexc(ADDR_ERR,RaiseSEGV);
  59.         } else {
  60.             Setexc(ILL_INSTR,OrgILL);
  61.             Setexc(PRIV_VIOL,OrgPRIV);
  62.             Setexc(DIV_ZERO,OrgFPE);
  63.             Setexc(BUS_ERR,OrgBUS);
  64.             Setexc(ADDR_ERR,OrgSEGV);
  65.         }
  66.     }
  67. }
  68.  
  69. static bool TrapsOn=FALSE;
  70.  
  71. void TrapExceptions(bool on)
  72. {
  73.     if (on!=TrapsOn) {
  74.         TrapsOn=on;
  75.         for (int sig=1; sig < NSIG; sig++) {
  76.             if (on) {
  77.                 signal(sig,SignalHandler);
  78.             } else {
  79.                 signal(sig,SIG_DFL);
  80.             }
  81.         }
  82.  
  83.         UnixifyAtariExceptions(on);
  84.     }
  85. }
  86.